package com.algorithm.liyc.combine;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 0046.全排列
 * 给定一个 没有重复 数字的序列，返回其所有可能的全排列。
 *
 * @author Liyc
 * @date 2024/1/23 14:43
 **/

public class Solution11 {
    private List<List<Integer>> result = new ArrayList<>();
    private LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0) {
            return result;
        }
        permute(nums, path);
        return result;
    }

    public void permute(int[] nums, LinkedList<Integer> path) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            // 如果path中已有，则跳过
            if (path.contains(nums[i])) {
                continue;
            }
            path.add(nums[i]);
            permute(nums, path);
            path.removeLast();
        }
    }
}
